package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDto;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.util.MybatisUtil;
import com.hzit.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {

	private SqlSessionFactory sessionFactory;
	public StudentServiceImpl(){
		try {
			sessionFactory = MybatisUtil.getSqlSessoinFactory();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Override
	public List<Student> findAll() {
		try(SqlSession session = sessionFactory.openSession()){
			final StudentMapper mapper = session.getMapper(StudentMapper.class);
			return mapper.selectByExample(null);
		}

	}
	//2. 条件查询
	@Override
	public List<Student> findStudents(StudentVo vo) {
		try(SqlSession session = sessionFactory.openSession()){
			final StudentMapper mapper = session.getMapper(StudentMapper.class);
			//1. 定义实例查询对象
			StudentExample example = new StudentExample();
			//2. 定义实例中的一次条件查询
			final StudentExample.Criteria criteria = example.createCriteria();
			if(vo != null){
				if(StrUtil.isNotBlank(vo.getSname())){
					criteria.andSnameLike("%" + vo.getSname() + "%");
				}
				if(StrUtil.isNotBlank(vo.getAddr())){
					criteria.andAddrLike("%" + vo.getAddr() + "%");
				}
				if(vo.getCid() != null && vo.getCid() != 0){
					criteria.andCidEqualTo(vo.getCid());
				}
			}
			return mapper.selectByExample(example);
		}


	}

	//3. 开始分页查询
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) {
		//3.1 开始分页查询
		PageHelper.startPage(page,pageSize);
		//3.2 开始查询
		try(SqlSession session = sessionFactory.openSession()) {
			final StudentMapper mapper = session.getMapper(StudentMapper.class);
			//3.3 查询出所有学生
			final List<Student> students = mapper.selectByExample(null);
			//3.4 转换为Page<Student>对象
			Page<Student> studentPage = (Page<Student>) students;
			//3.5 返回PageResult对象
			return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
		}

	}

	//4. 分页带条件查询
	@Override
	public PageResult<StudentDto> search(int page, int pageSize, StudentVo vo) throws InvocationTargetException, IllegalAccessException {
		//4.1 开始分页查询
		PageHelper.startPage(page,pageSize);
		//4.2 开始查询
		try(SqlSession session = sessionFactory.openSession()) {
			 StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//4.3 查询出所有学生
			StudentExample example = new StudentExample();
			//4.3.1 构造条件实例关联的查询条件对象
			final StudentExample.Criteria criteria = example.createCriteria();
			//4.3.2 根据传入的参数是否有值，决定添加条件
			if(vo != null){
				if(StrUtil.isNotBlank(vo.getSname())){
					criteria.andSnameLike("%" + vo.getSname() + "%");
				}
				if(StrUtil.isNotBlank(vo.getAddr())){
					criteria.andAddrLike("%" + vo.getAddr() + "%");
				}
				if(vo.getCid() != null && vo.getCid() != 0){
					criteria.andCidEqualTo(vo.getCid());
				}
			}
			List<Student> students = mapper.selectByExample(example);
			List<StudentDto> studentDtos = new ArrayList<>();
			//4.4 转换为Page<Student>对象
			Page<Student> studentPage = (Page<Student>) students;
			List<Student> studentList = studentPage.getResult();
			for (Student student : studentList) {
				StudentDto dto = new StudentDto();
				//属性拷贝（同名属性才可以拷贝），参数1：目标对象 参数2：原对象（有内容的）
				BeanUtils.copyProperties(dto,student);
				Integer cid = student.getCid();
				Classes classes = classesMapper.selectByPrimaryKey(cid);
				dto.setCname(classes.getCname());
				studentDtos.add(dto);
			}
			//4.5 返回PageResult对象
			return new PageResult<>(studentPage.getTotal(),studentDtos);
		}

	}
}
